package com.linzm.leetcode.primary.ilearning.demo0113;

import java.util.*;

/**
 * @Author zimingl
 * @Date 2023/1/14 0:04
 * @Description: 返回最终的单词个数
 */
public class Demo1 {
    public static void main(String[] args) {
        List<String> lines = new ArrayList<>();
        lines.add("   one,...   two  ... ..,, three  ");
        lines.add(" fou");
        lines.add("  r ,,...fiv\\");
        lines.add("  ,..e  six,");
        Demo1 demo01 = new Demo1();
        int num = demo01.setWords(lines);
        System.out.println(num);
    }

    private int setWords(List<String> lines) {
        List<Character> list = Arrays.asList(' ', '.', ',');
        String tmpString = "", firstString = "";
        boolean flag = false;
        int count = 0;
        List<Deque<String>> dequeList = new ArrayList<>();
        for (int j = 0; j < lines.size(); j++) {
            String line = lines.get(j);
            Deque<String> deque = new LinkedList<>();
            if (j >= 1 && dequeList.get(j - 1).getLast().contains("\\")) {
                flag = true;
            }
            for (int i = 0; i < line.length(); i++) {
                if (flag) {
                    if (list.contains(line.charAt(i))) {
                        firstString = firstString + line.charAt(i);
                    } else {
                        deque.add(firstString + line.charAt(i));
                        flag = false;
                    }
                } else {
                    if (!list.contains(line.charAt(i))) {
                        tmpString = tmpString + line.charAt(i);
                    }
                    if (!tmpString.equals("")
                            && (list.contains(line.charAt(i))
                            || i == line.length() - 1)) {
                        deque.add(tmpString);
                        count++;
                        tmpString = "";
                    }
                }
            }
            dequeList.add(deque);
        }
        for (int i = 0; i < dequeList.size(); i++) {
            if (dequeList.get(i).getLast().contains("\\") && i != dequeList.size() - 1) {
                if (dequeList.get(i + 1).getFirst().contains(",") || dequeList.get(i + 1).getFirst().contains(".")) {
                    count++;
                }
            }
        }
        return count;
    }
}
